﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Data;
using System.Linq;
using DPFP.Error;
using System.Diagnostics;

namespace BioAttendance
{
    public partial class RegistrationPage : Form
    {
        public RegistrationPage(BioAttendanceEntities dbEntities)
        {
            InitializeComponent();
            this.DbEntities = dbEntities;
            this.Employee = new Employee();
            this.employeeBindingSource.DataSource = this.Employee;
            this.Load += new EventHandler(RegistrationPage_Load);
            this.fingerPrintRegUserControl.RegistrationCompletedStatusChanged += new StatusChangedEventHandler(fingerPrintRegUserControl_RegistrationCompleted);
        }

        public Employee Employee;
        private BioAttendanceEntities DbEntities;

        #region EventHandler Members:

        private void RegistrationPage_Load(object sender, EventArgs e)
        {
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBoxEmpName.Text))
            {
                MessageBox.Show("Employee name is empty.");
                return;
            }
            this.Employee.Name = this.textBoxEmpName.Text;
            this.DbEntities.AddToEmployees(this.Employee);
            this.DbEntities.SaveChanges();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void fingerPrintRegUserControl_RegistrationCompleted(object sender, StatusChangedEventArgs e)
        {
            this.Employee.FingerPrint = e.Status ? this.fingerPrintRegUserControl.FingerPrint : null;
            this.registerButton.Enabled = e.Status;
        }

        private void WebcamButton_Click(object sender, EventArgs e)
        {
            var webcamDlg = new WebCamDailog();
            if (DialogResult.OK == webcamDlg.ShowDialog(this))
            {
                this.EmpPictureBox.Image = webcamDlg.photo;
                MemoryStream photo = new MemoryStream();
                this.EmpPictureBox.Image.Save(photo, System.Drawing.Imaging.ImageFormat.Jpeg);
                this.Employee.Photo = photo.ToArray();
            }
        }

        #endregion
    }
}
